<?php

namespace App\Library\BTC;

use App\Library\BlockIo;

/**
 * 第三方block.io接口（免费接口一天请求300次）
 * 二次封装
 */
class BitcoinFunction
{
    protected $block;

    public function __construct($currency)
    {
        switch (strtolower($currency)) {
            case 'btc': //比特币
                $this->block = new BlockIo('2d0f-435c-15a4-5fc1', 'qwe7788717a', 2);
                break;
            case 'ltc': //莱特币
                $this->block = new BlockIo('2021-7651-3f42-3940', 'qwe7788717a', 2);
                break;
            case 'dgc': //狗币
                $this->block = new BlockIo('34ad-6b58-c023-59bf', 'qwe7788717a', 2);
                break;
        }
    }

    /**
     * 创建一个新钱包地址
     * @return bool [type] [description]
     */
    public function createWalletAddress()
    {
        $res = $this->block->get_new_address();

        if ($res->status == 'success') {
            return $res->data->address;
        }

        return false;
    }

    /**
     * 获取钱包余额
     * @return bool [type] [description]
     */
    public function getWalletBalance()
    {
        $res = $this->block->get_balance();
        //返回可用余额
        if ($res->status == 'success') {
            return $res->data->available_balance;
        }

        return false;
    }

    /**
     * 获取地址余额
     * @param  [type] $address [description]
     * @return bool [type]          [description]
     */
    public function getBalanceByAddress($address)
    {
        $res = $this->block->get_address_balance(['address' => $address]);
        if ($res->status == 'success') {
            return $res->data->available_balance;
        }

        return false;
    }

    /**
     * 获取钱包的所有地址
     * @return array|bool [type] [description]
     */
    public function getAllAddress()
    {
        $res = $this->block->get_my_addresses();

        $data = [];
        if ($res->status == 'success') {
            $arr = $res->data->addresses;
            foreach ($arr as $k=>$v) {
                $data[$k] = $v->address;
            }
            return $data;
        }

        return false;
    }

    /**
     * 验证地址是否合法
     * @param  [type] $address [description]
     * @return bool [type]          [description]
     */
    public function addressValidator($address)
    {
        return $this->block->initkey()->validateAddress($address);
    }

    /**
     * 获取交易产生的手续费
     * @param $amounts
     * @param $address
     */
    public function tryGetProc($amounts, $address)
    {
        $this->block->get_network_fee_estimate(array('amounts' => $amounts, 'to_addresses' => $address));
    }

    /**
     * 转账
     * @param  [type] $amounts       [description]
     * @param  [type] $fromAddresses [description]
     * @param  [type] $toAddresses   [description]
     * @param  [type] $nonce         [description]
     * @return [type]                [description]
     */
    public function makeTransaction($amounts, $fromAddresses, $toAddresses, $nonce=null)
    {
        $array = [
            'amounts' => $amounts,
            'from_addresses' => $fromAddresses,
            'to_addresses' => $toAddresses,
            'nonce' => $nonce
        ];

        return $this->block->withdraw_from_addresses($array);
    }
}
